<?php
namespace MeetUp\Services;
//require_once 'vendor/autoload.php';

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;


class qiniu {
    /*
     * $img   图片的资源(base64)
     * $name  文件名的前缀
     */
    public function qiniu($img,$name){
        $base64 = preg_replace("/\s/",'+',$img);
        $accessKey = 'D2uFO0YA2TWy_GiqfuzOPU3nw8K47DfZVU0O3RVo';
        $secretKey = 'z9S8xldENUR_Uz760nB6OiKwRyOgGT4BSQHZCduU';
        $auth = new Auth($accessKey, $secretKey);
        $bucket = 'ta-meetup';
        $token = $auth->uploadToken($bucket);
        $uploadMgr = new UploadManager();
        // 上传文件到七牛
        $filePath = $base64;
        $key = date("$name.m-d-H:i:s-") . uniqid() . '.png';
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            return '';
        } else {
            return $ret['key'];
        }
    }

}
